package com.example.drools_study.demo2.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.drools_study.demo2.dto.CalculationDto;
import com.example.drools_study.demo2.req.CalculationReq;
import com.example.drools_study.demo2.vo.CalculationVo;
import com.example.drools_study.demo2.service.RuleService;
import org.drools.core.base.RuleNameEqualsAgendaFilter;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @Author: 郭浩伟 qq:912161367
 * @Date: 2022/11/18 0018 19:41
 * @Description:
 */
@Service
public class RuleServiceImpl implements RuleService {
    @Autowired
    private KieBase kieBase;

    /**
     * 调用Drools规则引擎实现个人所得税计算
     *
     * @param req
     * @return
     */
    @Override
    public List<CalculationVo> calculate(CalculationReq req) {
        KieSession session = kieBase.newKieSession();
        CalculationDto calculationDto = BeanUtil.copyProperties(req, CalculationDto.class);
        //初始化税后工资=税前工资
        calculationDto.setWageDeductedTax(calculationDto.getWageBeforeTax());
        //设置全局变量，名称和类型必须和规则文件中定义的全局变量名称对应
        session.setGlobal("calculationGlobalDto",calculationDto);
        session.fireAllRules(new RuleNameEqualsAgendaFilter("calculation"));
        session.dispose();
        return calculationDto.getCalculationVoList();
    }
}


